package com.example.education.service.impl;

import com.example.education.exception.ParamException;
import com.example.education.dao.UserMapper;
import com.example.education.model.User;
import com.example.education.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User login(String loginName, String password) throws ParamException {
        if(loginName==null){
            throw new ParamException("loginName为空");
        }else if(password==null){
            throw new ParamException("password为空");
        }
        return userMapper.login(loginName,password);
    }

    @Override
    public User register(String loginName, String password) throws ParamException {
        if(loginName==null){
            throw new ParamException("loginName为空");
        }else if(password==null){
            throw new ParamException("password为空");
        }
        User user = new User();
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setUserCate(1);
        Integer result =  userMapper.insertUser(user);
        if(result>0){
            return login(loginName,password);
        }else {
            return null;
        }
    }

    @Override
    public boolean updateUserInfo(User user) throws ParamException {
        if(user==null){
            throw new ParamException("参数为空");
        }else if(user.getUserId()==null){
            throw new ParamException("UserId为空");
        }
        Integer result =  userMapper.updateUserInfo(user);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public User getUserById(Integer id) throws ParamException {
        if(id==null){
            throw new ParamException("UserId为空");
        }
        return userMapper.getUserById(id);
    }

    @Override
    public List<User> getUserByIds(List<Integer> ids) throws ParamException {
        if (ids==null){
            throw new ParamException("ids为空");
        }
        return userMapper.getUserByIds(ids);
    }
}
